/**
 * Created by qqli on 5/27/15.
 */
'use strict';

Ctrl.controller('DashboardCtrl', ['$scope', '$location', '$rootScope', 'stcStatService',
    function ($scope, $location, $rootScope, Dashboard) {
        $rootScope.title = 'Dashboard';
        $rootScope.header = 'Dashboard Chart';
        $rootScope.path = "dashboard";
        $rootScope.activeLi();

        loadStyle(HOST + "styles/build/dashboard.min.css");
        loadScript(HOST + "bower_components/chart-js/Chart.min.js");

        $('footer a').html('Go to Statistics').attr('href', HOST + 'admin/#/statistics');

        var options = {
            animation: false,
            scaleFontSize: 10,
            tooltipFontSize: 12,
            tooltipCornerRadius: 2,
            scaleLineColor: "rgba(0,0,0,.06)",
            scaleShowGridLines: false,
            bezierCurve: false,
            pointDotStrokeWidth: 2,
            gradientFill: true
        };

        // only chrome use animation
        if (isChrome()) {
            $.extend(options, {
                animation: true
            });
        }

        var darkerOptions = $.extend({}, options, {
            darkerLine: true
        });

        var ddCtx = $('#day-data')[0].getContext("2d"),
            wdCtx = $('#week-data')[0].getContext("2d"),
            mdCtx = $('#month-data')[0].getContext("2d"),
            qdCtx = $('#quarterly-data')[0].getContext("2d");

        Dashboard.query().$promise.then(function (data) {
            if (data.status === 1) {
                var ddData = convertChartData(data.data.hours, 'hours'),
                    wdData = convertChartData(data.data.days, 'days'),
                    mdData = convertChartData(data.data.weeks, 'weeks'),
                    qdData = convertChartData(data.data.months, 'months');

                var ddChart = new Chart(ddCtx).Line(ddData.data, options),
                    wdChart = new Chart(wdCtx).Line(mdData.data, options),
                    mdChart = new Chart(mdCtx).Line(wdData.data, darkerOptions),
                    qdChart = new Chart(qdCtx).Line(qdData.data, darkerOptions);

            } else if (data.status === -1) {
                lozLogin();
            }
        });

    }]);